namespace PickGold.Charting
{
    using System;
    using System.Collections;
    using System.ComponentModel;

    internal static class ConverterHelper
    {
        public static Chart GetChartFromContext(ITypeDescriptorContext context)
        {
            if ((context != null) || (context.Instance != null))
            {
                IChartElement instance = context.Instance as IChartElement;
                if (instance.Common != null)
                {
                    return instance.Common.Chart;
                }
                IList list = context.Instance as IList;
                if ((list != null) && (list.Count > 0))
                {
                    instance = list[0] as IChartElement;
                    if (instance.Common != null)
                    {
                        return instance.Common.Chart;
                    }
                }
                Chart service = context.Instance as Chart;
                if (service != null)
                {
                    return service;
                }
                IServiceProvider provider = context.Instance as IServiceProvider;
                if (provider != null)
                {
                    service = provider.GetService(typeof(Chart)) as Chart;
                    if (service != null)
                    {
                        return service;
                    }
                }
            }
            return null;
        }
    }
}

